import elasticsearch
from elasticsearch import Elasticsearch

print(f"elasticsearch client version: {elasticsearch.__version__}")
es = Elasticsearch("http://39.108.176.217:9200")
if es.ping():
    print("connect success")
else:
    print("connect fail")
try:
    # 获取JSON格式的健康信息
    health_json = es.cat.health(
        v=True,
        format="json",  # 返回JSON格式
        time="30s"  # 超时设置
    )

    print("集群健康状态（结构化数据）:")
    if health_json:
        info = health_json[0]
        print(f"• 集群: {info.get('cluster', 'N/A')}")
        print(f"• 状态: {info.get('status', 'N/A')}")
        print(f"• 节点总数: {info.get('node.total', 'N/A')}")
        print(f"• 数据节点: {info.get('node.data', 'N/A')}")
        print(f"• 分片总数: {info.get('shards', 'N/A')}")
        print(f"• 主分片数: {info.get('pri', 'N/A')}")
        print(f"• 时间戳: {info.get('timestamp', 'N/A')}")

except Exception as e:
    print(f"错误: {e}")